package com.his.struts.util;

/**
 * 转换编码xiaoweiliang蛋蛋
 *
 */
public class TranCharset {
	 
	    /** 
	      * 将字符串编码格式转成UTF-8
	      * 
	      * @param str 
	      * @return 
	      */  
	    public static String TranEncodeTOUTF_8(String str) {   
	        try {   
	             String strEncode = TranCharset.getEncoding(str);  
	             String temp = new String(str.getBytes(strEncode), "UTF-8");   
	            return temp;   
	         } catch (java.io.IOException ex) {   
	  
	            return null;   
	         }   
	     }
	 
	    /** 
	      * 判断字符串的编码 
	      * 
	      * @param str 
	      * @return 
	      */  
	    public static String getEncoding(String str) {   
	         String encode = "GB2312";   
	        try {   
	            if (str.equals(new String(str.getBytes(encode), encode))) {   
	                 String s = encode;   
	                return s;   
	             }   
	         } catch (Exception exception) {   
	         }   
	         encode = "ISO-8859-1";   
	        try {   
	            if (str.equals(new String(str.getBytes(encode), encode))) {   
	                 String s1 = encode;   
	                return s1;   
	             }   
	         } catch (Exception exception1) {   
	         }   
	         encode = "UTF-8";   
	        try {   
	            if (str.equals(new String(str.getBytes(encode), encode))) {   
	                 String s2 = encode;   
	                return s2;   
	             }   
	         } catch (Exception exception2) {   
	         }   
	         encode = "GBK";   
	        try {   
	            if (str.equals(new String(str.getBytes(encode), encode))) {   
	                 String s3 = encode;   
	                return s3;   
	             }   
	         } catch (Exception exception3) {   
	         }   
	        return "";   
	     }   
	    
}
